<?php

namespace app\common\model;

use think\Model;

class Account extends Model
{
    protected $table = 'accounts';


    //用户账户
    public static function userAccounts($user_id)
    {
        $coins = Coin::select();

        $items = [];
        foreach ($coins as $coin) {
            $account = self::oneAccount($user_id, strtoupper($coin['name']));

            $account->worth_usdt = $account->available * 10;

            array_push($items, $account);
        }

        return $items;
    }


    //获取一个账户
    public static function oneAccount($user_id, $coin_name)
    {
        $account = self::where('user_id', $user_id)->where('coin_name', $coin_name)->find();
        if (!$account) {
            $account            = new Account();
            $account->user_id   = $user_id;
            $account->coin_name = $coin_name;
            $account->available = 0;
            $account->disabled  = 0;
            $account->save();
        }
        return $account;
    }


    //判断余额是否充足
    public static function checkBalance($user_id, $coin_name, $number)
    {
        $account = self::oneAccount($user_id, $coin_name);
        if ($account->available < $number) {
            return false;
        }
        return true;
    }

}
